{% extends 'index.html' %}
{% block main-content %}
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">考勤管理</h3>
        </div>
    </div>
    <br><br>
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="input-group" style="width: 600px">
                <label for="scontent"></label><input type="text" name="scontent" id="scontent" class="form-control" placeholder="Search By StuNum or StuName or ClassName" style="height: 50px">
                <span class="input-group-btn">
                    <button  class="btn btn-primary" id="btnkq_search" style="height: 50px;text-align: center">查找</button>
      </span>
            </div><!-- /input-group -->
        </div><!-- /.col-lg-6 -->
    </div><!-- /.row --><br>
    <hr style="border: black solid 1px">
{#    表格内容#}
    <table class="table table-hover table-striped">
        <thead>
        <tr>
            <th>ID</th>
            <th>姓名</th>
            <th>学号</th>
            <th>座位号</th>
            <th>座位坐标</th>
            <th>课室</th>
            <th>考勤时间</th>
            <th>状态</th>
        </tr>
        </thead>
        <tbody id="tbody">
        {% for kaoqin_obj in kaoqin_queryset %}
            <tr>
                <td>{{ forloop.counter }}</td>
                <td>{{ kaoqin_obj.stuname }}</td>
                <td>{{ kaoqin_obj.stunum }}</td>
                <td>{{ kaoqin_obj.seatnum }}</td>
                <td>{{ kaoqin_obj.seatcoordinates }}</td>
                <td>{{ kaoqin_obj.classid }}</td>
                <td>{{ kaoqin_obj.attendancetime }}</td>
                <td>{{ kaoqin_obj.attendanceresult }}</td>
                <td>
                    <a href="{% url 'tea_admin:kaoqin_edit' kaoqin_obj.attendid %}"
                       class="btn btn-primary btn-xs">编辑</a>
                    <button class="btn btn-danger btn-xs del"
                            delete_id="{{ kaoqin_obj.attendid }}">删除
                    </button>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
{#    分页#}
    <div class="col-md-offset-4">
        {{ kaoqin_obj.page_html|safe }}
    </div>
{#    操作ajax#}
    <script>
        $('.del').click(function () {
            {#alert($(this).attr('delete_id'));#}
            var currentbtn = $(this);
            $.ajax({
                url: '/tea_admin/kaoqin_del/',
                type: 'post',
                data: {"delete_id": currentbtn.attr('delete_id')},
                success: function (args) {
                    if (args.code === 200) {
                        alert(args.msg);
                        currentbtn.parent().parent().remove();
                    } else {
                        alert(args.errors);
                    }
                }
            })
        })
    </script>
    <script>
    $('#btnkq_search').click(function () {
        $.ajax({
            url:'/tea_admin/kaoqinsearch/',
            type: 'post',
            data:{
                "scontent":$('#scontent').val()
            },
            success:function (args) {
                {#$('#tbody').empty()#}
                $('#tbody').find('tr').remove()
                {#alert(args);#}
                args=JSON.parse(args)
                console.log(args)
                for (let i = 0; i < args.length; i++) {
                    data=args[i]
                    let html='<tr>\n' +
                    '                <td>'+ data.pk+'</td>\n' +
                    '                <td>'+ data.fields.stuname +'</td>\n' +
                    '                <td>'+ data.fields.stunum +'</td>\n' +
                    '                <td>'+ data.fields.seatnum +'</td>\n' +
                    '                <td>'+ data.fields.seatcoordinates +'</td>\n' +
                    '                <td>'+ data.fields.classid +'</td>\n' +
                    '                <td>'+ data.fields.attendancetime +'</td>\n' +
                    '                <td>'+ data.fields.attendanceresult +'</td>\n' +
                    '                <td>\n' +
                    '                    <a href="http://127.0.0.1:8000/tea_admin/kaoqin_edit/'+ data.pk +'"\n' +
                    '                       class="btn btn-primary btn-xs">编辑</a>\n' +
                    '                    <button class="btn btn-danger btn-xs del"\n' +
                    '                            delete_id='+ data.pk +'>删除\n' +
                    '                    </button>\n' +
                    '                </td>\n' +
                    '            </tr>'

                    $('#tbody').append(
                            html
                    )

            }}
        })
    })
    </script>
{% endblock %}
